//
//  QSViewCell.m
//  Gunz
//
//  Created by Joggy on 16/8/22.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "QSViewCell.h"
#import "QSViewCellTitle.h"
#import "QSViewCellOption.h"

@interface QSViewCell () {
    
    QSViewCellTitle *title;
    QSViewCellOption *option1;
    QSViewCellOption *option2;
    QSViewCellOption *option3;
    QSViewCellOption *option4;
    UITextView *shortAnswer;
    NSInteger singleOption;
    NSMutableDictionary *multipleOption;
    
    
}

@end

@implementation QSViewCell

- (instancetype)initWithAttributes:(NSDictionary *)dic {
    self = [super initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    _question = dic;
    _type = [_question[@"type"] integerValue];
    singleOption = -1;
    multipleOption = [NSMutableDictionary dictionaryWithCapacity:4];
    multipleOption[@"1"] = @"0";
    multipleOption[@"2"] = @"0";
    multipleOption[@"3"] = @"0";
    multipleOption[@"4"] = @"0";
    switch (_type) {
        case QSTypeSingle:
            [self addSingle];
            break;
        case QSTypeMultiple:
            [self addMultiple];
            break;
        case QSTypeShortAnswer:
            [self addShortAnswer];
            break;
        default:
            break;
    }
    
    return self;
}

- (void)addSingle {
    title = [[QSViewCellTitle alloc] initWithTitle:[NSString stringWithFormat:@"%@%@", _question[@"question"], @"(单选)"]];
    title.center = CGPointMake(self.frame.size.width/2, 22);
    [self addSubview:title];
    NSArray *options = _question[@"answer"];
    option1 = [[QSViewCellOption alloc] initWithOption:options[0]];
    option1.center = CGPointMake(self.frame.size.width/2, 22 + 44*1);
    option1.pointButton.tag = 1;
    [option1.pointButton addTarget:self action:@selector(singleAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:option1];
    option2 = [[QSViewCellOption alloc] initWithOption:options[2]];
    option2.center = CGPointMake(self.frame.size.width/2, 22 + 44*2);
    option2.pointButton.tag = 2;
    [option2.pointButton addTarget:self action:@selector(singleAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:option2];
    option3 = [[QSViewCellOption alloc] initWithOption:options[0]];
    option3.center = CGPointMake(self.frame.size.width/2, 22 + 44*3);
    option3.pointButton.tag = 3;
    [option3.pointButton addTarget:self action:@selector(singleAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:option3];
    option4 = [[QSViewCellOption alloc] initWithOption:options[0]];
    option4.center = CGPointMake(self.frame.size.width/2, 22 + 44*4);
    option4.pointButton.tag = 4;
    [option4.pointButton addTarget:self action:@selector(singleAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:option4];
    CGRect cFrame = self.frame;
    cFrame.size.height = option4.frame.origin.y + option4.frame.size.height;
    self.frame = cFrame;
}

- (void)addMultiple {
    title = [[QSViewCellTitle alloc] initWithTitle:[NSString stringWithFormat:@"%@%@", _question[@"question"], @"(多选)"]];
    title.center = CGPointMake(self.frame.size.width/2, 22);
    [self addSubview:title];
    NSArray *options = _question[@"answer"];
    option1 = [[QSViewCellOption alloc] initWithOption:options[0]];
    option1.center = CGPointMake(self.frame.size.width/2, 22 + 44*1);
    option1.pointButton.tag = 1;
    [option1.pointButton addTarget:self action:@selector(multipleAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:option1];
    option2 = [[QSViewCellOption alloc] initWithOption:options[2]];
    option2.center = CGPointMake(self.frame.size.width/2, 22 + 44*2);
    option2.pointButton.tag = 2;
    [option2.pointButton addTarget:self action:@selector(multipleAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:option2];
    option3 = [[QSViewCellOption alloc] initWithOption:options[0]];
    option3.center = CGPointMake(self.frame.size.width/2, 22 + 44*3);
    option3.pointButton.tag = 3;
    [option3.pointButton addTarget:self action:@selector(multipleAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:option3];
    option4 = [[QSViewCellOption alloc] initWithOption:options[0]];
    option4.center = CGPointMake(self.frame.size.width/2, 22 + 44*4);
    option4.pointButton.tag = 4;
    [option4.pointButton addTarget:self action:@selector(multipleAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:option4];
    CGRect cFrame = self.frame;
    cFrame.size.height = option4.frame.origin.y + option4.frame.size.height;
    self.frame = cFrame;
}

- (void)addShortAnswer {
    title = [[QSViewCellTitle alloc] initWithTitle:_question[@"question"]];
    title.center = CGPointMake(self.frame.size.width/2, 22);
    [self addSubview:title];
    shortAnswer = [[UITextView alloc] initWithFrame:CGRectMake(44, 48, self.frame.size.width - 68, 100)];
    shortAnswer.layer.cornerRadius = 6;
    shortAnswer.layer.borderWidth = 1;
    shortAnswer.layer.borderColor = CUSTOM_GREEN.CGColor;
    [self addSubview:shortAnswer];
    CGRect cFrame = self.frame;
    cFrame.size.height = shortAnswer.frame.origin.y + shortAnswer.frame.size.height;
    self.frame = cFrame;
}

- (void)singleAction:(UIButton*)button {
    option1.pointButton.selected = false;
    option2.pointButton.selected = false;
    option3.pointButton.selected = false;
    option4.pointButton.selected = false;
    button.selected = !button.selected;
    singleOption = button.tag;
}

- (void)multipleAction:(UIButton*)button {
    button.selected = !button.selected;
    NSString *dat = [NSString stringWithFormat:@"%ld", (long)button.tag];
    if(button.selected) {
        multipleOption[dat] = @"1";
    }
    else {
        multipleOption[dat] = @"0";
    }
}

- (NSDictionary*)answer {
    if(_answer) {
        _answer = nil;
    }
    
    switch (_type) {
        case QSTypeSingle:
            _answer = @{
                        @"type": @"1",
                        @"answer": @[[NSString stringWithFormat:@"%ld", (long)singleOption]]
                        };
            break;
        case QSTypeMultiple:
        {
            NSMutableArray *multi = [NSMutableArray arrayWithCapacity:4];
            for(NSString *str in [multipleOption keyEnumerator]) {
                if([[multipleOption valueForKey:str] isEqualToString:@"1"]) {
                    [multi addObject:str];
                }
            }
            
            _answer = @{
                        @"type": @"2",
                        @"answer": multi
                        };
        }
            break;
        case QSTypeShortAnswer:
            _answer = @{
                        @"type": @"3",
                        @"answer": shortAnswer.text
                        };
            break;
        default:
            break;
    }
    
    return _answer;
}

@end
